package com.example.demo.consumer;


import com.alibaba.fastjson.JSON;
import com.example.demo.constant.MqConstant;
import com.example.demo.dao.OrderDao;
import com.example.demo.entity.Order;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import sun.rmi.runtime.Log;

/**
 * 消费订单消息
 */
@Component
@Slf4j
@RocketMQMessageListener(consumerGroup = MqConstant.ORDER_CONSUMER,topic =MqConstant.ORDER_TOPIC )
public class OrderConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    OrderDao orderDao;


    @Override
    public void onMessage(MessageExt messageExt) {
        log.info("监听到的消息为{}",messageExt);
        String orderMessage = new String(messageExt.getBody());
        Order order = JSON.parseObject(orderMessage, Order.class);
        log.info("解析的消息主体为{}",order);

        //将订单信息插入到数据库
        orderDao.insert(order);
    }
}
